/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.collisions;

import java.io.Serializable;

import org.easyway.interfaces.sprites.ICollisionTester;
import org.easyway.interfaces.sprites.ISpriteColl;
import org.easyway.lists.BaseList;
import org.easyway.objects.Type;
import org.easyway.system.StaticRef;
import org.easyway.utils.Utility;

public class BruteForceCollision implements ICollisionTester, Serializable {
	static final long serialVersionUID = 1001;

	public BaseList<ISpriteColl> list = new BaseList<ISpriteColl>();

	public Type type;

	public boolean isPrecise = true;

	protected boolean isAdded;

	protected boolean autoGroupped;

	public BruteForceCollision(boolean isToAdd, String name) {
		if (isAdded = isToAdd)
			StaticRef.coreCollision.groups.add(this);
		type = new Type(name);
	}

	public BruteForceCollision() {
		this(true, null);
	}

	public BruteForceCollision(String name) {
		this(true, name);
	}

	public BruteForceCollision(boolean isToAdd) {
		this(isToAdd, null);
	}

	public void loop() {
		ISpriteColl sourceObj;
		ISpriteColl destinationObj;
		if (isPrecise) { // effettua il test solo 1 volta
			// for (int i = source.size() - 1; i >= 0; i--) {

			for (int i = list.size() - 1; i > 0; --i) {
				// if ((sourceObj = ((ISpriteColl) source.get(i))) == null)
				sourceObj = list.get(i);
				// continue;
				// for (int j = destination.size() - 1; j >= 0; j--) {
				for (int j = i - 1; j >= 0; --j) {
					// if ((destinationObj = ((ISpriteColl) destination.get(j)))
					// == null)
					// continue;
					destinationObj = list.get(j);
					// se NON sei roteato
					if (sourceObj.getRotation2D() == 0
							&& destinationObj.getRotation2D() == 0) {
						if (CollisionUtils.rectangleHit(sourceObj,
								destinationObj)) {
							if (CollisionUtils.trueHit(sourceObj,
									destinationObj))
								CollisionUtils
										.hitted(sourceObj, destinationObj);

						}
					} else if (CollisionUtils.circleHit(sourceObj,
							destinationObj)) {
						if (CollisionUtils.trueHitAlpha(sourceObj,
								destinationObj, sourceObj.getRotation2D(),
								destinationObj.getRotation2D()))
							CollisionUtils.hitted(sourceObj, destinationObj);
					}

				}
			}
		} else {
			for (int i = list.size() - 1; i > 0; --i) {
				if ((sourceObj = list.get(i)) == null)
					continue;
				for (int j = i - 1; j >= 0; --j) {
					if ((destinationObj = list.get(j)) == null)
						continue;
					if (CollisionUtils.rectangleHit(sourceObj, destinationObj)) {
						CollisionUtils.hitted(sourceObj, destinationObj);
					}
				}
			}
		}
	}
	
	public void add(ISpriteColl spr) {
		list.add(spr);
	}
	
	public void remove(ISpriteColl spr) {
		if (spr==null)
			return;
		list.remove(spr);
	}

	public Type getType() {
		return type;
	}

	public void setType(String type) { // 0.1.9
		this.type.set(type);
	}

	public void destroy() {
		if (isAdded)
			StaticRef.coreCollision.groups.remove(this);
		if (autoGroupped)
			StaticRef.coreCollision.autoGroups.remove(this);

	}

	// --------------------------

	public static BruteForceCollision createGroup(String name) {
		return createGroup(name, true);
	}

	public static BruteForceCollision createGroup(String name, boolean isToAdd) {
		BruteForceCollision temp = new BruteForceCollision(isToAdd, name);
		temp.autoGroupped = true;
		StaticRef.coreCollision.autoGroups.add(temp);
		return temp;
	}

	public static BruteForceCollision getGroup(String name) {
		ICollisionTester temp;
		if ((temp = StaticRef.coreCollision.search(name)) != null)
			try {
				return (BruteForceCollision) temp;
			} catch (Exception e) {
				Utility.error("Error on casting to BruteForceCollision",
						"BruteForceCollision.getGroup", e);
				return null;
			}
		Utility.error("type not found", "BruteForceCollision.getGroup");
		return null;
	}

}
